import Mock from 'mockjs'
// 品类
var data = {};
// 商品
var info = {};
// 订单
var menu = {};

//品类 - 生成数据
if (localStorage.getItem('class_list')) {
    data = JSON.parse(localStorage.getItem('class_list'))
} else {
    //生成数据
    data = Mock.mock({
        "list|100": [
            {
                id: '@id',
                name: '@pick(["大牌专区","优惠专区","买买买专区","满减专区"])',
                date: '@date',
                sonName: '@ctitle(3, 6)',
            }
        ]
    })
    //保存数据到本地存储
    localStorage.setItem('class_list', JSON.stringify(data))
}
//品类 - 查询接口
Mock.mock('/api/class/list', 'get', () => {
    //返回数据
    return data;
})
//品类 - 删除接口
Mock.mock('/api/class/delete', 'post', (req) => {
    var { id } = JSON.parse(req.body);
    //通过id, 找到要删除的那一条数据, 执行删除
    var i = data.list.findIndex((item) => {
        return item.id == id;
    })
    if (i != -1) {
        data.list.splice(i, 1);
        localStorage.setItem('class_list', JSON.stringify(data));
    }
    return { status: 200, msg: '删除成功!', list: data.list }
})
//品类 - 编辑接口
Mock.mock('/api/class/edit', 'post', (req) => {
    var elist = (JSON.parse(req.body)).editlist
    // var { id } = JSON.parse(req.body);
    // //通过id, 找到要编辑的那一条数据, 执行编辑
    var i = data.list.findIndex((item) => {
        return item.id == elist.id;
    })
    if (i != -1) {
        data.list[i] = elist
    }
    localStorage.setItem('class_list', JSON.stringify(data.list));
    return { status: 200, msg: '修改数据成功!', list: data.list }
})
// 品类 - 选择接口
Mock.mock('/api/select/list', 'get', () => {
    return data;
})
//品类 - 搜索接口
Mock.mock('/api/search/list', 'get', () => {
    return data;
})



// 商品 - 信息的数据
if (localStorage.getItem('goods_list')) {
    info = JSON.parse(localStorage.getItem('goods_list'))
} else {
    //生成数据
    info = Mock.mock({
        "list|10": [
            {
                id: '@id',
                name: '@ctitle(3,6)',
                title: '@ctitle(20, 50)',
                price: '@integer( 100, 500 )',
                count: '@integer( 100, 1500 )'
            }
        ]
    })
    //保存数据到本地存储
    localStorage.setItem('goods_list', JSON.stringify(info))
}

//商品 - 查询接口
Mock.mock('/api/goods/list', 'get', () => {
    //返回数据
    return info;
})

// 商品 - 删除接口
Mock.mock('/api/goods/delete', 'post', (req) => {
    var { id } = JSON.parse(req.body);
    //通过id, 找到要删除的那一条数据, 执行删除
    var i = info.list.findIndex((item) => {
        return item.id == id;
    })
    if (i != -1) {
        info.list.splice(i, 1);
        localStorage.setItem('goods_list', JSON.stringify(info));
    }
    return { status: 200, msg: '删除成功!', list: info.list }
})

//商品 - 编辑接口
Mock.mock('/api/goods/edit', 'post', (req) => {
    var elist = (JSON.parse(req.body)).editlist
    // //通过id, 找到要编辑的那一条数据, 执行编辑
    var i = info.list.findIndex((item) => {
        return item.id == elist.id;
    })
    if (i != -1) {
        info.list[i] = elist
    }
    localStorage.setItem('goods_list', JSON.stringify(info.list));
    return { status: 200, msg: '修改数据成功!', list: info.list }
})


//订单 - 生成数据
if (localStorage.getItem('order_list')) {
    menu = JSON.parse(localStorage.getItem('order_list'))
} else {
    //生成数据
    menu = Mock.mock({
        "list|100": [
            {
                id: '@id',
                name: '@ctitle(3,6)',
                price: '@integer( 100, 500 )',
                date: '@date',
                'phone|13000000000-19999999999': 1,
                way: '@pick(["未支付","微信","支付宝"])',
                status: '@pick(["待付款","已发货","待发货"])',
            }
        ]
    })
    //保存数据到本地存储
    localStorage.setItem('order_list', JSON.stringify(menu))
}
//订单 - 查询接口
Mock.mock('/api/order/list', 'get', () => {
    //返回数据
    return menu;
})



